package com.xiaoxu.privatechat.controller.admin;

import com.xiaoxu.privatechat.bean.MsgVo;
import com.xiaoxu.privatechat.bean.PageBean;
import com.xiaoxu.privatechat.bean.Result;
import com.xiaoxu.privatechat.entity.common.Account;
import com.xiaoxu.privatechat.entity.common.Friend;
import com.xiaoxu.privatechat.entity.common.MsgContent;
import com.xiaoxu.privatechat.service.common.AccountService;
import com.xiaoxu.privatechat.service.common.FriendService;
import com.xiaoxu.privatechat.service.common.MsgContentService;
import com.xiaoxu.privatechat.service.common.MsgLogService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @author xiaoxu
 * @description 后台消息管理控制器
 * @date 2023/2/13
 */
@RequestMapping("/admin/msg")
@Controller
public class MsgController {

    @Resource
    private AccountService accountService;

    @Resource
    private FriendService friendService;

    @Resource
    private MsgContentService msgContentService;

    @Resource
    private MsgLogService msgLogService;

    /**
     * @description 消息列表页面
     * @param model 模型
     * @param msgContent 消息内容
     * @param pageBean 分页
     * @return String
     */
    @RequestMapping(value = "/list")
    public String list(Model model, MsgContent msgContent, PageBean<MsgContent> pageBean) {
        model.addAttribute("title", "消息清單");
        model.addAttribute("msg", msgContent.getMsg());
        model.addAttribute("pageBean", msgContentService.findList(msgContent, pageBean));
        return "admin/msg_content/list";
    }

    /**
     * @description 查看消息
     * @param id 消息id
     * @return Result
     */
    @RequestMapping(value = "/view_info", method = RequestMethod.POST)
    @ResponseBody
    public Result<MsgVo> viewInfo(@RequestParam(name = "id") Long id,
                                  @RequestParam(name = "fromId") Long fromId,
                                  @RequestParam(name = "toId") Long toId,
                                  @RequestParam(name = "type") String type) {
        MsgVo msgVo = new MsgVo();
        msgVo.setMsgLogs(msgLogService.findByMsgContentId(id));
        if (MsgContent.CHAT_TYPE_SINGLE.equals(type)) {
            Friend friend = friendService.find(fromId);
            if (friend != null) {
                msgVo.setSender(friend.getFriendAccount().getUsername());
            }
            Account account = accountService.find(toId);
            if (account != null) {
                msgVo.setReciever(account.getUsername());
            }
            return Result.success(msgVo);
        }
        //表示群消息
        Account account = accountService.find(fromId);
        if (account != null) {
            msgVo.setSender(account.getUsername());
        }
        return Result.success(msgVo);
    }
}
